<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>call_myspace_api (MySpace::MySpaceContext)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File myspace/context/MySpaceContext.rb, line 103</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">call_myspace_api</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">params</span> = {}, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
      <span class="ruby-identifier">params</span> = <span class="ruby-identifier">params</span>.<span class="ruby-identifier">dup</span>
      <span class="ruby-identifier">ep</span> = <span class="ruby-constant">EndPoint</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">name</span>)
      <span class="ruby-identifier">url</span> = <span class="ruby-identifier">ep</span>.<span class="ruby-identifier">compute_path</span>(<span class="ruby-identifier">params</span>)
      <span class="ruby-identifier">timeout</span> = <span class="ruby-identifier">params</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:timeout</span>) <span class="ruby-operator">||</span> <span class="ruby-constant">TIMEOUT_SECS</span>
      <span class="ruby-comment cmt">#body = ''</span>
      
      <span class="ruby-identifier">body</span> = <span class="ruby-identifier">params</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:body</span>)
      <span class="ruby-identifier">headers</span> = <span class="ruby-identifier">params</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:headers</span>)
      
      <span class="ruby-identifier">params</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:v1_json</span>)
      
      <span class="ruby-constant">MySpaceContext</span>.<span class="ruby-identifier">remove_empty_strings</span>(<span class="ruby-identifier">params</span>)
      <span class="ruby-identifier">query_str</span> = <span class="ruby-identifier">params</span>.<span class="ruby-identifier">collect</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
          <span class="ruby-constant">CGI</span>.<span class="ruby-identifier">escape</span>(<span class="ruby-identifier">key</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">+</span> <span class="ruby-value str">'='</span> <span class="ruby-operator">+</span> <span class="ruby-constant">CGI</span>.<span class="ruby-identifier">escape</span>(<span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_s</span>) 
        <span class="ruby-keyword kw">end</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">'&amp;'</span>)
      

      <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value str">'?'</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">query_str</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">query_str</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>

      <span class="ruby-identifier">resp</span> = <span class="ruby-keyword kw">nil</span>
      <span class="ruby-ivar">@http_logger</span>.<span class="ruby-identifier">info</span>(<span class="ruby-node">&quot;sending: '#{url}'&quot;</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@http_logger</span>
      <span class="ruby-keyword kw">begin</span>
        <span class="ruby-constant">Timeout</span><span class="ruby-operator">::</span><span class="ruby-identifier">timeout</span>(<span class="ruby-identifier">timeout</span>, <span class="ruby-constant">TimeoutException</span>) <span class="ruby-keyword kw">do</span>
          <span class="ruby-identifier">resp</span> = <span class="ruby-ivar">@access_token</span>.<span class="ruby-identifier">request</span>(<span class="ruby-identifier">ep</span>.<span class="ruby-identifier">method</span>, <span class="ruby-identifier">url</span>, <span class="ruby-identifier">body</span>, <span class="ruby-identifier">headers</span>)
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">TimeoutException</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
        <span class="ruby-identifier">e</span>.<span class="ruby-identifier">timeout</span> = <span class="ruby-identifier">timeout</span>
        <span class="ruby-identifier">e</span>.<span class="ruby-identifier">url</span> = <span class="ruby-identifier">url</span>
        <span class="ruby-identifier">raise</span> <span class="ruby-identifier">e</span>
      <span class="ruby-keyword kw">end</span>
      <span class="ruby-ivar">@http_logger</span>.<span class="ruby-identifier">info</span>(<span class="ruby-node">&quot;received: '#{resp.code}': '#{resp.body}'&quot;</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@http_logger</span>

      <span class="ruby-identifier">validate_response</span>(<span class="ruby-identifier">resp</span>, <span class="ruby-identifier">url</span>)

      <span class="ruby-identifier">content_type</span> = <span class="ruby-identifier">resp</span>[<span class="ruby-value str">'content-type'</span>]
      <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">content_type</span>
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">content_type</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/json/</span>
          <span class="ruby-keyword kw">return</span> <span class="ruby-constant">JSON</span><span class="ruby-operator">::</span><span class="ruby-identifier">parse</span>(<span class="ruby-identifier">resp</span>.<span class="ruby-identifier">body</span>)
        <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">content_type</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/xml/</span>
          <span class="ruby-keyword kw">return</span> <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">resp</span>.<span class="ruby-identifier">body</span>)
        <span class="ruby-keyword kw">end</span>
        
        <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;unknown content type: #{content_type}&quot;</span>
      <span class="ruby-keyword kw">end</span>
    <span class="ruby-keyword kw">end</span></pre>
</body>
</html>